import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressTest {
    public static void main(String[] args) {
        try {
            InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
            //toString方法输出主机名和IP地址
            System.out.println(inetAddress.toString());
            //获取ip地址
            String ip = inetAddress.toString().split("/")[1];
            //根据Ip地址获取主机名
            InetAddress byAddress = InetAddress.getByName(ip);
            System.out.println(byAddress.getCanonicalHostName());
            System.out.println(byAddress.getHostName());
            System.out.println(byAddress.getHostAddress());
            System.out.println("localhost:"+InetAddress.getLocalHost());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
